package com.cxlin.cwanandroid.ui.login

import android.app.ProgressDialog
import android.os.Bundle
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import com.cxlin.base.BaseActivity
import com.cxlin.base.ext.binding
import com.cxlin.base.ext.toast
import com.cxlin.base.mvi.ext.collectFLow
import com.cxlin.cwanandroid.R
import com.cxlin.cwanandroid.databinding.ActivityLoginBinding
import com.cxlin.cwanandroid.ui.login.viewmodel.LoginViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/29 13:43
 */
class LoginActivity : BaseActivity() {

    private val binding by binding<ActivityLoginBinding>(R.layout.activity_login)
    private val viewModel by viewModel<LoginViewModel>()

    override fun initView(savedInstanceState: Bundle?) {
        binding.mToolbar.title = "登录"
        binding.mToolbar.setNavigationIcon(R.drawable.arrow_back)
        binding.mToolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        binding.userNameEt.addTextChangedListener(onTextChanged = { _, _, _, _ ->
            viewModel.sendIntent(LoginViewModel.LoginUiIntent.CheckEnableLoginBtn(
                binding.userNameEt.text.toString(),
                binding.passwordEt.text.toString()
            ))
        })
        binding.passwordEt.addTextChangedListener(onTextChanged = { _, _, _, _ ->

            viewModel.sendIntent(LoginViewModel.LoginUiIntent.CheckEnableLoginBtn(
                binding.userNameEt.text.toString(),
                binding.passwordEt.text.toString()
            ))
        })
        binding.login.setOnClickListener {
            viewModel.sendIntent(
                LoginViewModel.LoginUiIntent.Login(
                    binding.userNameEt.text.toString(),
                    binding.passwordEt.text.toString()))
        }
        binding.register.setOnClickListener {
            viewModel.sendIntent(
                LoginViewModel.LoginUiIntent.Register(
                    binding.userNameEt.text.toString(),
                    binding.passwordEt.text.toString()))
        }
    }

    override fun observeStateAndEvent() {
        viewModel.uiStateFlow.collectFLow(this){
            when(this){
                is LoginViewModel.LoginUiState.Idel -> {}
                is LoginViewModel.LoginUiState.IsLoading -> {
                    if (this.result) {
                        showProgressDialog()
                    } else {
                        dismissProgressDialog()
                    }
                }
                is LoginViewModel.LoginUiState.IsSuccess -> {
                    if (this.result) finish()
                }
                is LoginViewModel.LoginUiState.EnableLoginBtn -> {
                    binding.login.isEnabled = this.result
                    binding.register.isEnabled = this.result
                }
                else -> {}
            }
        }
        viewModel.effectFlow.collectFLow(this){
            when(this){
                is LoginViewModel.LoginEvent.ToastEvent -> {
                    this@LoginActivity.toast(
                        this.message,
                        if (this.short) Toast.LENGTH_SHORT else Toast.LENGTH_LONG
                    )
                }
            }
        }
    }

    private var progressDialog: ProgressDialog? = null
    private fun showProgressDialog() {
        if (isFinishing) return

        if (progressDialog == null)
            progressDialog = ProgressDialog(this)
        progressDialog?.show()
    }

    private fun dismissProgressDialog() {
        progressDialog?.dismiss()
        progressDialog = null
    }

    override fun onDestroy() {
        dismissProgressDialog()
        super.onDestroy()
    }
}